package gw.com.android.ui.gesture_pwd;


import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.gwtsz.gts2.hx.R;

import java.util.List;

import butterknife.BindView;
import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import gw.com.android.presenter.PresenterImpl;
import gw.com.android.ui.gesture_pwd.base.GestureBaseActivity;
import gw.com.android.ui.gesture_pwd.custom.EasyGestureLockLayout;
import www.com.library.dialog.ToastPopWindow;

/**
 * 手势密码 设置界面
 */
public class GesturePwdSettingActivity extends GestureBaseActivity implements PresenterImpl {

    @BindView(R.id.layout_small)
    EasyGestureLockLayout layout_small;
    @BindView(R.id.tv_go)
    TextView tv_go;
    @BindView(R.id.tv_redraw)
    TextView tv_redraw;
    @BindView(R.id.layout_parent)
    EasyGestureLockLayout layout_parent;
    @BindView(R.id.tv_skip)
    TextView tv_skip;


    @Override
    public void onRequestSuc(Object result) {

    }

    @Override
    public void onRequestFail(int failCode) {

    }

    @Override
    public void onBackPressed() {
        //这里不接受back按键
        animate(tv_go);
    }

    @Override
    protected int getLayoutView() {
        return R.layout.layout_gesture_pwd_setting;
    }

    @Override
    protected void initLayoutView() {

        EasyGestureLockLayout.GestureEventCallbackAdapter adapter = new EasyGestureLockLayout.GestureEventCallbackAdapter() {
            @Override
            public void onSwipeFinish(List<Integer> pwd) {
                layout_small.refreshPwdKeyboard(pwd);//通知另一个小密码盘，将密码点展示出来，但是不展示轨迹线
                tv_redraw.setVisibility(View.VISIBLE);
            }

            @Override
            public void onResetFinish(List<Integer> pwd) {// 当密码设置完成
                savePwd(showPwd("showGesturePwdInt", pwd));//保存密码到本地
                new ToastPopWindow(GesturePwdSettingActivity.this, AppMain.getAppString(R.string.gesture_pwd_set_success)).show();//弹窗表示，设置成功
                GTConfig.instance().setBooleanValue(GTConfig.PRE_OPEN_GESTURE, true);//把 系统设置“是否启用手势密码”标志位设置为true
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();//然后结束自己
                    }
                }, 500);
            }

            @Override
            public void onCheckFinish(boolean succeedOrFailed) {
                String str = succeedOrFailed ? "解锁成功" : "解锁失败";
                new ToastPopWindow(GesturePwdSettingActivity.this, str).show();
                if (succeedOrFailed) {//如果解锁成功，则切换到set模式
                    layout_parent.switchToResetMode();
                } else {
                    onCheckFailed();
                }
            }

            @Override
            public void onSwipeMore() {
                //执行动画
                animate(tv_go);
            }

            @Override
            public void onToast(String s, int textColor) {
                tv_go.setText(s);
                if (textColor != 0)
                    tv_go.setTextColor(textColor);

                if (textColor == 0xffFF3232) {
                    animate(tv_go);
                }
            }
        };

        layout_parent.setGestureFinishedCallback(adapter);

        //使用rest模式
        layout_parent.switchToResetMode();

        tv_skip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();//跳过？结束自己吧;
            }
        });
        tv_redraw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layout_parent.initCurrentTimes();
                tv_redraw.setVisibility(View.INVISIBLE);
                layout_small.refreshPwdKeyboard(null);
                tv_go.setText("请重新绘制");
            }
        });
    }

    @Override
    protected void initViewData() {

    }


}
